package com.gss.system.controller;

import com.gss.common.core.controller.BaseController;
import com.gss.entity.R;
import com.gss.entity.enums.ConstantEnum;
import com.gss.entity.sys.dto.SysMenu;
import com.gss.entity.sys.dto.SysUser;
import com.gss.entity.sys.vo.SysMenuVo;
import com.gss.entity.sys.vo.SysUserDeptVo;
import com.gss.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Random;


/**
 * @author DYang
 * @date 2023/6/17
 */
@Api(tags = "用户管理")
@CrossOrigin
@RestController
@RequestMapping("/user")
public class SysUserController extends BaseController<SysUser> {

    @Autowired
    private ISysUserService sysUserService;

    @ApiOperation(value = "注册",tags = "用户管理")
    @PostMapping("/register")
    public R<String> register(@RequestBody SysUser entity) {
        return this.insert(entity);
    }

    @ApiOperation(value = "发送验证码",tags = "用户管理")
    @GetMapping("/sendCaptcha")
    public R<Integer> sendCaptcha(String email) {
        int randomNumber = new Random().nextInt(900000) + 100000;
        return R.ok(randomNumber);
    }


    @ApiOperation(value = "获取授权菜单",tags = "用户管理")
    @GetMapping("/permissionRoutes")
    public R<List<SysMenuVo>> permissionRoutes(String roles) {
        if (!StringUtils.hasText(roles)) {
            return R.error(ConstantEnum.PARAM_ERROR);
        }
        return R.ok(sysUserService.permissionRoutes(roles));
    }

    @Override
    @ApiOperation(value = "单个新增")
    @PostMapping("/insert")
    public R<String> insert(@RequestBody SysUser entity) {
        if (entity == null|| entity.getUserName() == null || entity.getPassword() == null || entity.getEmail() == null) {
            return R.error(ConstantEnum.PARAM_ERROR);
        }
        // 判断是否已存在
        SysUser user = sysUserService.getUserByName(entity.getUserName());
        if (user != null) {
            return R.error(ConstantEnum.INSERT_ERROR.getCode(),"用户名已存在");
        }

        boolean save = sysUserService.save(entity);
        if (save) {
            return R.ok(ConstantEnum.SUCCESS);
        }
        return R.error(ConstantEnum.INSERT_ERROR);
    }

    @ApiOperation(value = "用户授权角色",tags = "用户管理")
    @PostMapping("/oauthDeptRole")
    public R<String> oauthDeptRole(@RequestBody SysUserDeptVo entity){
        if (entity == null) {
            return R.error(ConstantEnum.PARAM_ERROR);
        }
        return sysUserService.oauthDeptRole(entity);
    }

    @ApiOperation(value = "用户撤销角色",tags = "用户管理")
    @DeleteMapping("/deleteDeptRole")
    public R<String> deleteDeptRole(@RequestBody List<Integer> idList){
        if (idList == null) {
            return R.error(ConstantEnum.PARAM_ERROR);
        }
        return sysUserService.deleteDeptRole(idList);
    }

    @ApiOperation(value = "获取用户信息",tags = "用户管理")
    @GetMapping("/userInfo")
    public R<SysUserDeptVo> userInfo(String username){
        if (username == null) {
            return R.error(ConstantEnum.PARAM_ERROR);
        }
        return sysUserService.userInfo(username);
    }

}
